package com.example.rule.toolkit;

import com.example.rule.common.enums.ConditionLogicEnum;
import com.example.rule.entity.dto.ConditionSaveDTO;
import com.example.rule.entity.po.ConditionPO;

import java.util.List;

public class ExpressionBuilder {

    /**
     * 拼接多个条件表达式
     *
     * @param conditions 条件列表
     * @param logicDesc 逻辑关系（"全都满足"/"任意满足"/"都不满足"）
     * @return 完整表达式字符串
     */
    public static String buildExpression(List<ConditionPO> conditions, String logicDesc) {
        if (conditions == null || conditions.isEmpty()) {
            return "";
        }

        String operator = ConditionLogicEnum.getOperatorByDescription(logicDesc);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < conditions.size(); i++) {
            ConditionPO conditionPO = conditions.get(i);
            String expr = conditionPO.getExpression();

            if (i > 0) {
                sb.append(" ").append(operator).append(" ");
            }
            sb.append(expr);
        }

        // 如果是“都不满足”，则整体取反
        if ("都不满足".equals(logicDesc)) {
            return "!(" + sb.toString() + ")";
        }

        return sb.toString();
    }
}